package org.xacmlauth.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class ContextManager {
	
//	private static final Log LOG = LogFactory.getLog(ContextManager.class);
	
	private static final ThreadLocal <JAXBContext> policyLocal = new ThreadLocal<JAXBContext>(){
        protected JAXBContext initialValue() {
            try {
//            	LOG.info("Creating thread local instance");
				return JAXBContext.newInstance(Constants.POLICY_CONTEXT_PATH);
			} catch (JAXBException e) {
//				LOG.error("Policy context creation error", e);
			}
            return null;
        }
    };
    
    private static final ThreadLocal <JAXBContext> contextLocal = new ThreadLocal<JAXBContext>(){
        protected JAXBContext initialValue() {
            try {
//            	LOG.info("Creating thread local instance");
				return JAXBContext.newInstance(Constants.REQUEST_CONTEXT_PATH);
			} catch (JAXBException e) {
//				LOG.error("Policy context creation error", e);
			}
            return null;
        }
    };
    
    private static final ThreadLocal <JAXBContext> domainLocal = new ThreadLocal<JAXBContext>(){
        protected JAXBContext initialValue() {
            try {
//            	LOG.info("Creating thread local instance");
				return JAXBContext.newInstance(Constants.DOMAIN_CONTEXT_PATH);
			} catch (JAXBException e) {
//				LOG.error("Policy context creation error", e);
			}
            return null;
        }
    };
    
    public static JAXBContext getPolicyContext(){
//    	LOG.info("Retrieving thread local instance");
    	return policyLocal.get();
    }
    
    public static JAXBContext getRequestContext(){
//    	LOG.info("Retrieving thread local instance");
    	return contextLocal.get();
    }
    
    public static JAXBContext getDomainContext(){
//    	LOG.info("Retrieving thread local instance");
    	return domainLocal.get();
    }
    
}
